/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.filter;

import com.blandware.atleap.webapp.util.core.GlobalProperties;
import com.blandware.atleap.webapp.util.core.WebappConstants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>Filter to apply Cache-Control HTTP header if it does not exist in order to allow browser to cache mapped resource</p>
 * <p>Also it applies character encoding for all filtered requests</p>
 * <p/>
 * <p><a href="BrowserCacheFilter.java.html"><i>View Source</i></a></p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.4 $ $Date: 2005/08/24 07:29:42 $
 * @web.filter name="browserCacheFilter"
 */
public class BrowserCacheFilter implements Filter  {

    protected FilterConfig config = null;

    /**
     * Initializes the filter with a given filter config
     *
     * @param config The config to use
     * @throws ServletException
     */
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    /**
     * Destroys the filter.
     */
    public void destroy() {
        config = null;
    }

    /**
     * Filters a request: adds cache control headers
     *
     * @param req   Filtered request
     * @param resp  Response that will be result of filtering
     * @param chain Chain of following filters
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain)
            throws IOException, ServletException {
        // cast to the types I want to use
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        int maxAge = ((Integer) GlobalProperties.getInstance(config.getServletContext()).getInteger(WebappConstants.CACHE_RESOURCE_MAXAGE_PROPERTY, -1)).intValue();
        if ( maxAge >= 0 ) {
            response.setHeader("Cache-Control", "public,max-age=" + maxAge);
            response.setHeader("Pragma", "");
        } else {
            response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
            response.setHeader("Pragma", "no-cache");
        }

        response.setCharacterEncoding("utf-8");

        chain.doFilter(request, response);
    }
}
